package cn.song.controller;

import cn.song.constants.enums.GenderEnum;
import cn.song.model.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 枚举转换控制器
 * 提供枚举相关的API接口
 *
 * @author SongRenShuo
 * @date 2025/01/06
 */
@Slf4j
@RestController
@RequestMapping("/api/enum")
public class EnumController {

    /**
     * 获取性别枚举列表
     *
     * @return 性别枚举列表
     */
    @GetMapping("/gender/list")
    public R<List<Map<String, Object>>> getGenderList() {
        try {
            List<Map<String, Object>> genderList = new ArrayList<>();
            
            for (GenderEnum gender : GenderEnum.values()) {
                Map<String, Object> genderMap = new HashMap<>();
                genderMap.put("code", gender.getCode());
                genderMap.put("name", gender.getName());
                genderList.add(genderMap);
            }
            
            return R.success(genderList);
        } catch (Exception e) {
            log.error("获取性别枚举列表失败", e);
            return R.failed("获取性别枚举列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据编码获取性别枚举
     *
     * @param code 性别编码
     * @return 性别枚举信息
     */
    @GetMapping("/gender/{code}")
    public R<Map<String, Object>> getGenderByCode(@PathVariable Integer code) {
        try {
            GenderEnum gender = GenderEnum.valueOf(code);
            if (gender == null) {
                return R.failed("未找到对应的性别枚举");
            }
            
            Map<String, Object> genderMap = new HashMap<>();
            genderMap.put("code", gender.getCode());
            genderMap.put("name", gender.getName());
            
            return R.success(genderMap);
        } catch (Exception e) {
            log.error("根据编码获取性别枚举失败", e);
            return R.failed("根据编码获取性别枚举失败：" + e.getMessage());
        }
    }

    /**
     * 根据名称获取性别枚举
     *
     * @param name 性别名称
     * @return 性别枚举信息
     */
    @GetMapping("/gender/name/{name}")
    public R<Map<String, Object>> getGenderByName(@PathVariable String name) {
        try {
            GenderEnum gender = GenderEnum.valueOfName(name);
            if (gender == null) {
                return R.failed("未找到对应的性别枚举");
            }
            
            Map<String, Object> genderMap = new HashMap<>();
            genderMap.put("code", gender.getCode());
            genderMap.put("name", gender.getName());
            
            return R.success(genderMap);
        } catch (Exception e) {
            log.error("根据名称获取性别枚举失败", e);
            return R.failed("根据名称获取性别枚举失败：" + e.getMessage());
        }
    }

    /**
     * 性别编码转换测试
     *
     * @param code 性别编码
     * @return 转换结果
     */
    @PostMapping("/gender/convert")
    public R<Map<String, Object>> convertGender(@RequestParam Integer code) {
        try {
            GenderEnum gender = GenderEnum.valueOf(code);
            if (gender == null) {
                return R.failed("无效的性别编码");
            }
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", gender.getCode());
            result.put("name", gender.getName());
            result.put("originalCode", code);
            
            return R.success(result);
        } catch (Exception e) {
            log.error("性别编码转换失败", e);
            return R.failed("性别编码转换失败：" + e.getMessage());
        }
    }

    /**
     * 性别名称转换测试
     *
     * @param name 性别名称
     * @return 转换结果
     */
    @PostMapping("/gender/convert/name")
    public R<Map<String, Object>> convertGenderByName(@RequestParam String name) {
        try {
            GenderEnum gender = GenderEnum.valueOfName(name);
            if (gender == null) {
                return R.failed("无效的性别名称");
            }
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", gender.getCode());
            result.put("name", gender.getName());
            result.put("originalName", name);
            
            return R.success(result);
        } catch (Exception e) {
            log.error("性别名称转换失败", e);
            return R.failed("性别名称转换失败：" + e.getMessage());
        }
    }
} 